import Toast from "@vant/weapp/toast/toast";

// pages/sethouse/sethouse.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    obj: {}
  },
  async set() {
    if (!(/^1[3-9]\d{9}$/.test(this.data.obj.mobile))) return Toast('手机号格式错了');
    let a = JSON.parse(JSON.stringify(this.data.obj))
    delete a.status
    const { data } = await wx.http.post(`/room`, a)
    wx.navigateTo({
      url: '/pages/house/house'
    })
  },
  chooseImage() {
    wx.chooseImage({
      success: (res) => {
        wx.uploadFile({
          url: 'https://live-api.itheima.net/upload',
          filePath: res.tempFilePaths[0],
          name: 'image',
          header: {
            'Content-Type': 'multipart/form-data',
            'Authorization': 'Bearer ' + wx.getStorageSync('token'),
          },
          success: (res) => {
            this.setData({
              'obj.idcardBackUrl': JSON.parse(res.data).data.url
            });
          },
        })
      }
    })
  },
  chooseImage2() {
    wx.chooseImage({
      success: (res) => {
        wx.uploadFile({
          url: 'https://live-api.itheima.net/upload',
          filePath: res.tempFilePaths[0],
          name: 'image',
          header: {
            'Content-Type': 'multipart/form-data',
            'Authorization': 'Bearer ' + wx.getStorageSync('token'),
          },
          success: (res) => {
            console.log(res)
            this.setData({
              'obj.idcardFrontUrl': JSON.parse(res.data).data.url
            });
          },
        })
      }
    })
  },
  imageclose() {
    this.setData({
      'obj.idcardBackUrl': ''
    })
  },
  imageclose2() {
    this.setData({
      'obj.idcardFrontUrl': ''
    })
  },
  nameinput(e: any) {
    this.setData({
      'obj.name': e.detail.value
    })
  },
  mobileinput(e: any) {
    this.setData({
      'obj.mobile': e.detail.value
    })
  },
  handleGenderChange(e: any) {
    this.setData({
      'obj.gender': e.detail.value
    })
  },
  async onLoad(options) {
    if (options.point) {
      this.setData({
        'obj.point': options.point,
        'obj.building': options.building,
        'obj.room': options.room,
        'obj.idcardFrontUrl': '',
        'obj.idcardBackUrl': '',
      })
      return
    }
    const { data } = await wx.http.get(`/room/${decodeURIComponent(options.id)}`)
    this.setData({
      obj: data
    })
  },
})